/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fpt.dal;

import edu.fpt.common.Budget;
import edu.fpt.common.HibernateUtil;
import edu.fpt.common.Users;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author BlueS
 */
public class BudgetDAL {

    public static List listAll(String flag) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            Query query = session.createQuery("from Budget where Kind = :code order by ID DESC");
            query.setParameter("code", flag);
            List list = query.list();

            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }

    public static List listByTime(String year, String month, String kind) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Calendar calCurr = GregorianCalendar.getInstance();
            calCurr.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
            int day = calCurr.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
            Query query = session.createQuery("from Budget where CreatedTime >= :code and CreatedTime <= :code2 and Kind = :code3 order by ID DESC");
            query.setParameter("code", year + "-" + month + "-" + 1);
            query.setParameter("code2", year + "-" + month + "-" + day);
            query.setParameter("code3", kind);
            List list = query.list();

            tran.commit();
            return list;
        } catch (Exception e) {
            return null;
        } finally {
            session.close();
        }
    }

    public static boolean checkLogin(String user, String pass) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            Query query = session.createQuery("from Users where Username = :code and Password = :code2");
            query.setParameter("code", user);
            query.setParameter("code2", pass);
            List list = query.list();

            tran.commit();

            if (list.size() > 0) {
                return true;
            } else {
                return false;
            }
        } finally {
            session.close();
        }
    }

    public static boolean checkUser(String user) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            Query query = session.createQuery("from Users where Username = :code");
            query.setParameter("code", user);
            List list = query.list();

            tran.commit();

            if (list.size() > 0) {
                return true;
            } else {
                return false;
            }
        } finally {
            session.close();
        }
    }

    public static void insertUser(Users entity) {
        Session session = null;
        Transaction tran = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            session.save(entity);

            tran.commit();
        } finally {
            session.close();
        }
    }

    public static void insertBudget(Budget entity) {
        Session session = null;
        Transaction tran = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            session.save(entity);

            tran.commit();
        } finally {
            session.close();
        }
    }

    public static void updateBudget(Budget entity) {
        Session session = null;
        Transaction tran = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            session.update(entity);

            tran.commit();
        } finally {
            session.close();
        }
    }

    public static Budget searchBudget(String id) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            Query query = session.createQuery("from Budget where ID = :code");
            query.setParameter("code", id);
            List list = query.list();

            tran.commit();

            if (list.size() > 0) {
                return (Budget) list.get(0);
            } else {
                return null;
            }
        } finally {
            session.close();
        }
    }

    public static void deleteBudget(String id) {
        Session session = null;
        Transaction tran = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();

            if (searchBudget(id) != null) {
                session.delete(searchBudget(id));
                tran.commit();
            }



        } finally {
            session.close();
        }
    }
}
